/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.unicauca.opentextbook.fachadas;

import edu.unicauca.opentextbook.entidades.Tarea;
import edu.unicauca.opentextbook.entidades.Tarea;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.LockTimeoutException;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import javax.persistence.PessimisticLockException;
import javax.persistence.Query;
import javax.persistence.QueryTimeoutException;
import javax.persistence.TransactionRequiredException;

/**
 *
 * @author Gamificaion
 */
@Stateless
public class TareaFacade extends AbstractFacade<Tarea> {

    @PersistenceContext(unitName = "openTextBookPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TareaFacade() {
        super(Tarea.class);
    }

    public List<Tarea> consultarTareasPorLibro(int idLibro) {
        final StringBuilder sql = new StringBuilder();
        Query query;
        List<Tarea> listaTareas = null;

        sql.append("SELECT * ");
        sql.append("FROM tarea t ");
        sql.append("WHERE t.idLibro = ? ");
        sql.append("ORDER BY t.idTarea");

        try {
            query = em.createNativeQuery(sql.toString(), Tarea.class);
            query.setParameter(1, idLibro);

            listaTareas = query.getResultList();
        } catch (NoResultException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_NoResultException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (IllegalStateException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_IllegalStateException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (QueryTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_QueryTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (TransactionRequiredException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_TransactionRequiredException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (PessimisticLockException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_PessimisticLockException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (LockTimeoutException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_LockTimeoutException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (PersistenceException e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_PersistenceException:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "ERROR_DESARROLLO_consultarTareasPorLibro_Exception:".concat(e.getMessage()));
            listaTareas = new ArrayList<Tarea>();
        } finally {
            return listaTareas;
        }
    }
}
